package cn.picclife.mwx.manager.sys.service.impl;

import cn.picclife.mwx.manager.sys.model.bo.UserSessionBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import cn.picclife.mwx.manager.sys.mapper.SysMaintainMapper;
import cn.picclife.mwx.manager.sys.mapper.SysMaintainVersionMapper;
import cn.picclife.mwx.manager.sys.model.bo.PageCount;
import cn.picclife.mwx.manager.sys.model.bo.SysMaintainReq;
import cn.picclife.mwx.manager.sys.model.po.SysMaintain;
import cn.picclife.mwx.manager.sys.service.SysMaintainService;

import javax.servlet.http.HttpServletRequest;

@Service
public class SysMaintainServiceImpl implements SysMaintainService{

	@Autowired
	private SysMaintainMapper sysMaintainMapper;
	
	@Autowired
	private SysMaintainVersionMapper sysMaintainVersionMapper;
	
	/**
	 * 保存信息
	 */
	public void saveSysMaintain(SysMaintainReq req) {
		
		SysMaintain maintain = new SysMaintain();
		maintain.setId(UUID.randomUUID().toString());
		maintain.setSystemNo(req.getSystemNo());
		maintain.setSystemName(req.getSystemName());
		maintain.setSystemChargePop(req.getSystemChargePop());
		maintain.setSystemPhone(req.getSystemPhone());
		maintain.setSystemPhoneType(req.getSystemPhoneType());
		maintain.setSystemUrl(req.getSystemUrl());
		maintain.setSystemDateUrl(req.getSystemDateUrl());
		maintain.setSystemFlagStr(req.getSystemFlagStr());//系统标识串
		
		maintain.setServerConStr(req.getServerConStr());//服务器连接串
		maintain.setPassword(req.getPassword());//密码
		maintain.setFileUrl(req.getFileUrl());//项目服务器目录
		maintain.setFileName(req.getFileName());//项目服务器文件名
		maintain.setAutoFlag(req.getAutoFlag());//是否自动备份文件
		sysMaintainMapper.saveSysMaintain(maintain);
	}
	
	/**
	 * 获取分页数据
	 * @param req
	 * @return
	 */
	public PageCount querySysMaintain(SysMaintainReq req, HttpServletRequest request) {
		
		PageCount pageCount = new PageCount();
		pageCount.setNowPage(req.getNowPage());//当前页
		pageCount.setRowNum(req.getRowNum());//每页多少条
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("systemNo", req.getSystemNo());//系统编码
		paramMap.put("systemName", req.getSystemName());//系统名称
		paramMap.put("systemChargePop", req.getSystemChargePop());//负责人
		UserSessionBo userSessionBo = (UserSessionBo) request.getSession().getAttribute("user");
		String deptNo = userSessionBo.getDeptNo();
		if("2".equals(userSessionBo.getDeptLevel())){
			paramMap.put("deptNo", deptNo);
		}
		int allCount = sysMaintainMapper.queryAllCount(paramMap);
		if(0==pageCount.getRowNum()) {
			pageCount.setRowNum(10);
		}
		if(0==pageCount.getNowPage()) {
			pageCount.setNowPage(1);
		}
		int allPages = allCount / pageCount.getRowNum();

		if (allCount % pageCount.getRowNum() > 0) {
			allPages += 1;//总页数
		}

		pageCount.setAllPages(allPages);
		pageCount.setAllCount(allCount);
		int minTotal = (pageCount.getNowPage() - 1) * pageCount.getRowNum();
		int maxTotal = pageCount.getNowPage() * pageCount.getRowNum();
		pageCount.setMinTotal(minTotal);
		pageCount.setMaxTotal(maxTotal);
		pageCount.setMaxminTotal(maxTotal-minTotal);
		pageCount.setObject(paramMap);
		List<SysMaintain> sysMaintainList = sysMaintainMapper.querySysMaintainPage(pageCount);
		pageCount.setDatas(sysMaintainList);
		return pageCount;
	}
	
	/**
	 * 更新
	 */
	public int updateSysMaintain(SysMaintainReq req) {
		
		SysMaintain maintain = new SysMaintain();
		maintain.setId(req.getId());
		maintain.setSystemNo(req.getSystemNo());//系统编码
		maintain.setSystemName(req.getSystemName());//系统名称
		maintain.setSystemChargePop(req.getSystemChargePop());//负责人
		maintain.setSystemPhone(req.getSystemPhone());//联系方式
		maintain.setSystemPhoneType(req.getSystemPhoneType());//终端类型
		maintain.setSystemUrl(req.getSystemUrl());//项目地址URL
		maintain.setSystemDateUrl(req.getSystemDateUrl());//项目业务数据获取地址
		
		maintain.setServerConStr(req.getServerConStr());//服务器连接串
		maintain.setPassword(req.getPassword());//密码
		maintain.setFileUrl(req.getFileUrl());//项目服务器目录
		maintain.setFileName(req.getFileName());//项目服务器文件名
		maintain.setAutoFlag(req.getAutoFlag());//是否自动备份文件
		return sysMaintainMapper.updateSysMaintain(maintain);
	}
	
	/**
	 * 删除
	 */
	@Override
	public int deleteSysMaintain(List<String> list) {
		// TODO Auto-generated method stub
		int num = sysMaintainMapper.deleteSysMaintain(list);
		return num;
	}
//	public boolean ftpConnect(SysMaintainReq req) throws Exception {
//		
//		String serverConStr = req.getServerConStr();//服务器连接串 user@ip:port
//		String[] serv = serverConStr.split("@");
//		String[] ip_port = serv[1].split(":");
//		String userName = serv[0];
//		String ip = ip_port[0];
//		int port = Integer.valueOf(ip_port[1]);
//		String passWord = req.getPassword();
//		return FtpFileUtil.ftpConnect(ip, port, userName, passWord);
//	}

//	public static void ftpDownload1() {
////		String userName = "tianqiftp";//ftp服务器的用户名
////		String ip = "9.23.24.170";// ftp服务器的ip地址
////		int ports = 21;// ftp服务器的端口号
////		String passWord = "e05Con25Omy";// ftp服务器的密码
//		String userName = "ftpadmin";//ftp服务器的用户名
//		String ip = "192.168.1.199";// ftp服务器的ip地址
//		int ports = 21;// ftp服务器的端口号
//		String passWord = "123456";// ftp服务器的密码
//		String ftpFileUrl="/home/ftpadmin";//ftp文件地址
//		String localPath = "D:\\wenjian";//本地文件路径
//		String fileName = "wwww.zip";
//		boolean a = false;
//		a = FtpFileUtil.retrieveFile(ip, ports, userName, passWord, ftpFileUrl, fileName, localPath,null);
//		System.out.println("ftp下载结果"+a);
//		
//		SysmaintainVersion maintainVersion = new SysmaintainVersion();
////		sysMaintainVersionMapper.querySysMaintainVersion(maintain)
////		sysMaintainVersionMapper.saveSysMaintainVersion(maintain);
//	}
	
//	public static void main(String[] args) {
//		ftpDownload1();
//	}


}
